<template>
    <view>
        <u-form ref="validateFormRef" :model="validateForm.value" :label-width="150">
            <u-form-item label="姓名" prop="username" required>
                <u-input v-model="validateForm.value.username" placeholder="请输入手机号"/>
            </u-form-item>
            <u-form-item label="性别" prop="password" required>
                <u-input v-model="validateForm.value.password" placeholder="请输入密码"/>
            </u-form-item>
        </u-form>
        <u-button type="error" @click="submit">提交</u-button>
    </view>
</template>

<script>

    import {CommonUtil} from "../../dev/core/utils/common.util";

    export default {
        data() {
            return {
                validateForm:  {
                    value: {
                        username: '',
                        password: ''
                    },
                    rules: {
                        username: [
                            {required: true, message: '请输入名称!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '请输入名称!', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (value !== 1) {
                                        callback(new Error('输入名称错误！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],
                        password: [
                            {required: true, message: '请输入密码!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '请输入密码!', trigger: ['change', 'blur']}
                        ]
                    }
                }
            }
        },
        onReady() {
            this.$refs.validateFormRef.setRules(this.validateForm.rules);
        },
        methods: {
            submit() {
                const _self = this;
                this.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确认提交吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定

                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    };
</script>

<style lang="scss" scoped>
</style>
